package com.ruoyi.crop.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.crop.api.domain.Farmer;
import com.ruoyi.crop.api.domain.Goods;
import com.ruoyi.crop.api.domain.MaterialLot;
import com.ruoyi.crop.api.domain.Product;
import com.ruoyi.crop.service.ProductService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 产品Controller
 *
 * @author ruoyi
 * @date 2024-07-27
 */
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController
{
    @Autowired
    private ProductService productService;

    /**
     * 查询产品列表
     */
    @RequiresPermissions("system:product:list")
    @GetMapping("/list")
    public TableDataInfo list(Product product)
    {
        startPage();
        List<Product> list = productService.selectProductList(product);
        return getDataTable(list);
    }

    @RequiresPermissions("system:product:list")
    @GetMapping("/productList")
    public TableDataInfo selectDelFlagList(Product product)
    {
        startPage();
        List<Product> list = productService.selectDelFlagList(product);
        return getDataTable(list);
    }

    /**
     * 获取产品详细信息
     */
    @RequiresPermissions("system:product:query")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Integer productId)
    {
        return success(productService.getById(productId));
    }
    /**
     * 获取产品详细信息
     */
    @GetMapping(value = "getHuiGoods/{productId}")
    public AjaxResult getHuiGoods(@PathVariable("productId") Integer productId)
    {
        List<Integer> gids = productService.selectProductByGids(productId);
        return success(gids);
    }

    /**
     * 导出产品列表
     */
    @Log(title = "产物", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,Product product)
    {
        List<Product> list = productService.selectProductList(product);
        ExcelUtil<Product> util = new ExcelUtil<Product>(Product.class);
        util.exportExcel(response, list, "产品数据");
    }
    /**
     * 新增产品
     */
    @RequiresPermissions("system:product:add")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Product product)
    {
        product.setCreateBy(SecurityUtils.getUsername());
        product.setCreateTime(new Date());
        return toAjax(productService.saveAll(product));
    }

    /**
     * 修改产品
     */
    @RequiresPermissions("system:product:edit")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Product product)
    {
        product.setUpdateBy(SecurityUtils.getUsername());
        product.setUpdateTime(new Date());
        return toAjax(productService.updateProduct(product));
    }

    /**
     * 删除产品
     */
    @RequiresPermissions("system:product:remove")
    @Log(title = "产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Integer[] productIds)
    {
        return toAjax(productService.removeBatchByIds(Arrays.asList(productIds)));
    }
    /**
     * 删除产品
     */
    @RequiresPermissions("system:product:remove")
    @Log(title = "产品", businessType = BusinessType.DELETE)
    @DeleteMapping("delProductById/{productIds}")
    public AjaxResult delProductById(@PathVariable Integer[] productIds)
    {
        return toAjax(productService.delProductById(Arrays.asList(productIds)));
    }

    /**
     * 还原产品
     */
    @RequiresPermissions("system:product:remove")
    @Log(title = "产品", businessType = BusinessType.DELETE)
    @GetMapping("removeDelFlagIds/{productIds}")
    public AjaxResult removeDelFlagIds(@PathVariable Integer[] productIds)
    {
        return toAjax(productService.removeDelFlagIds(Arrays.asList(productIds)));
    }


    /**
     * 计算天数
     * @param one
     * @param two
     * @return
     * @throws ParseException
     */
    @PutMapping("selectDays/{one}/{two}")
    public AjaxResult selectDays(@PathVariable(value = "one") String one,@PathVariable(value = "two") String two) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse1 = simpleDateFormat.parse(one);
        Date parse2 = simpleDateFormat.parse(two);
        long between = DateUtil.between(parse1, parse2, DateUnit.DAY);
        System.err.println(between);
        return success(between);
    }
    /**
     * 农户名称
     */
    @GetMapping("FarmerList")
    public AjaxResult FarmerList(){
        List<Farmer> farmers = productService.selectFarmerList();
        return success(farmers);
    }
    /**
     * 批次号名称
     */
    @GetMapping("MaterialLotList")
    public AjaxResult MaterialLotList(){
        List<MaterialLot> materialLots = productService.selectMaterialLotList();
        return success(materialLots);
    }
}
